// 登录
import { userLogin, permissions, userLogout, userInfo, btnPermissions, getUserMenu, roles } from '@/api/user'
import constant from '@/common/constant/httpStatus'
import store from '@/store'

const state = {
  token: null,
  userInfo: {},
  permissions: [],
  btnPermissions: [],
  menu: [
    { key: '1', label: '首页', to: '/',icon:'setting'},
    {
      key: '2',
      label: 'financeMgr',
      to: '/finance',
      icon:'setting',
      children: [
        { key: '2-1', label: '交易统计', to: '/finance/trade' },
        { key: '2-2', label: '风控金统计', to: '/finance/risk' }
      ]
    },
    {
      key: '3',
      label: 'merMgr',
      to: '/merMgr',
      icon:'setting',
      children: [
        { key: '3-1', label: '商户GM用户管理', to: '/mer/gmMgr' },
        { key: '3-2', label: '商户余额变动记录', to: '/mer/balanceChange' },
        { key: '3-3', label: '认证管理', to: '/mer/certificationManagement' },
        { key: '3-4', label: '银行卡认证', to: '/mer/agentCertificationManagement' },
        { key: '3-5', label: '安全中心', to: '/mer/personalCenter' },
        { key: '3-6', label: '子账户配置', to: '/mer/subAccount' },
      ]
    },
    {
      key: '4',
      label: 'orderManagement',
      to: '/orderManagement',
      icon:'setting',
      children: [
        { key: '4-1', label: '订单管理', to: '/ordermgr/order' },
        { key: '4-2', label: '订单管理agent', to: '/ordermgr/agentOrder' },
      ]
    },
    {
      key: '5',
      label: 'profitsharingManagent',
      to: '/profitsharingManagent',
      icon:'setting',
      children: [
        { key: '5-1', label: '分润订单', to: '/profitSharing/audit' },
        { key: '5-2', label: '批次上传', to: '/profitSharing/batchNo' },
        { key: '5-3', label: '批次详情', to: '/profitSharing/batchNoDetail' },
      ]
    },
    {
      key: '6',
      label: '设置',
      to: '/system',
      icon:'setting',
      children: [
        { key: '6-1', label: '字典管理', to: '/dictionary' },
        { key: '6-2', label: '账号列表', to: '/userManagementList' },
        { key: '6-3', label: '新增账号', to: '/userManagementAdd' },
        { key: '6-4', label: '角色列表', to: '/roleManagementList' },
        { key: '6-5', label: '新增角色', to: '/roleManagementAdd' },
        { key: '6-6', label: '菜单权限管理', to: '/rightsManagement' },
        { key: '6-7', label: '日志管理', to: '/logManagement' },
        { key: '6-8', label: '系统设置', to: '/sysSetting' },
        { key: '6-9', label: '个人信息', to: '/userInfo' },

      ]
    },
  ],
  rootSubmenuKeys: [],//所有展开的key项（最外层）
  openKeys: [],//二层展开项
  openSelectedKeys:[],//里层展开项
  roles: [],
  secret: ''
}

const mutations = {
  SET_rootSubmenuKeys: (state, rootSubmenuKeys) => {
    state.rootSubmenuKeys = rootSubmenuKeys
  },
  SET_openKeys: (state, openKeys) => {
    state.openKeys = openKeys
  },
  SET_openSelectedKeys: (state, openSelectedKeys) => {
    state.openSelectedKeys = openSelectedKeys
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_SECRET: (state, secret) => {
    state.secret = secret
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  SET_BTN_PERMISSIONS: (state, btnPermissions) => {
    state.btnPermissions = btnPermissions
  },
  SIGN_OUT: (state) => {
    state.token = null
    state.userInfo = {}
    state.permissions = []
    state.btnPermissions = []
    state.roles = []
    state.menu = [
    ]
    state.secret = ''
    store.state.system.pageTabs = []
  },
  SET_MENU: (state, menu) => {
    state.menu = menu
  }
}

const actions = {

  async getRoles ({ commit }) {
    try {
      const { code, data } = await roles()
      if (constant.SUCCESS === code) {
        commit('SET_ROLES', data)
        return data
      }
      return []
    } catch (e) {
      return []
    }
  },
  async getMenu ({ commit }, userInfo) {
    try {
      const { data, code } = await getUserMenu()
      if (constant.SUCCESS === code) {
        commit('SET_MENU', data)
        return data
      }
      return []
    } catch (e) {
      return []
    }
  },
  async login ({ commit }, userInfo) {
    try {
      commit('SIGN_OUT')
      const { data, code } = await userLogin(userInfo)
      if (constant.SUCCESS === code) {
        commit('SET_TOKEN', data)
        return code
      }
      return null
    } catch (e) {
      return null
    }
  },
  async logout ({ commit }) {
    try {
      await userLogout()
      commit('SIGN_OUT')
    } catch (e) {
    }
  },
  async getInfo ({ commit }) {
    try {
      const { data, code } = await userInfo()
      if (constant.SUCCESS === code) {
        console.log(data)
        commit('SET_USER_INFO', data)
      }
    } catch (e) {
    }
  },
  async getPermission ({ commit }) {
    try {
      const { code, data } = await permissions()
      if (constant.SUCCESS === code) {
        commit('SET_PERMISSIONS', data)
        return data
      }
      return []
    } catch (e) {
      return []
    }
  },
  async getBtnPermission ({ commit }) {
    try {
      const { data, code } = await btnPermissions()
      if (constant.SUCCESS === code) {
        commit('SET_BTN_PERMISSIONS', data)
        return data
      }
      return []
    } catch (e) {
      return []
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
// 登录：当用户填写完账号和密码后向服务端验证是否正确，验证通过之后，服务端会返回一个token，拿到token之后（我会将这个token存贮到cookie中，保证刷新页面后能记住用户登录状态），前端会根据token再去拉取一个 user_info 的接口来获取用户的详细信息（如用户权限，用户名等等信息）。
// 权限验证：通过token获取用户对应的 role，动态根据用户的 role 算出其对应有权限的路由，通过 router.addRoutes 动态挂载这些路由。
